﻿using System;
using System.Collections.Generic;
using Medianamik.UI.Web.MVP.Presentation;
using StructureMap;
using StructureMap.Pipeline;

namespace Medianamik.UI.Web.MVP.View
{
    public abstract class PageModuleViewWithPresenter<T> : PageModuleView, IPageModuleViewWithPresenter
        where T : class
    {
        protected T Presenter;

        public override event EventHandler Initialize;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Presenter = NewPresenter();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            OnInitialize(e);
        }

        private void OnInitialize(EventArgs e)
        {
            if (Initialize != null)
                Initialize(this, e);
        }

        protected virtual T NewPresenter()
        {
            return ObjectFactory
                .GetInstance<T>(new ExplicitArguments(new Dictionary<string, object> { { "view", this } }));
        }

        public virtual IPageModulePresenter ViewPresenter
        {
            get
            {
                return Presenter as IPageModulePresenter;
            }
        }
    }
}